<template>
    <TabGrayBarCard :title="'服务商'" @onMore="goDetail()" :tabs="card.tabs" :curTab="card.curTab"
        :tabRight="card.tabRight" @onChange="changeTab" :grayCardList="card.grayCardList"
        :echartId="`myEChartsBar-${props.companyPlatformId}-service`" :echartData="card.echartData" :lineText="''" :barText="card.barText"
        :unitText="`单位：${card.unitText}`" :updateText="card.updateText">
    </TabGrayBarCard>
    <!-- v-btnPermi="{ permi: props.permi, noReport: true, isHiden: true }" -->
</template>

<script setup>
import TabGrayBarCard from '@/DbComponents/tabGrayBarCard'
import { toThousands, dateFormat } from '@/utils/number-util'
import dashBoardApi from '@/api/EnterpriseServices/dashBoard'
import { reactive, watch, computed } from 'vue'
import { useRouter } from 'vue-router'
const router = useRouter()
// import { reportLog } from '@/utils/reportLog'

const props = defineProps({
    companyPlatformId: {
        type: String,
        required: false,
        default: ''
    },
    updateDate: {
        type: String,
        required: false,
        default: ''
    }
})
const card = reactive(
    {
        tabs: [{ label: '分账', value: '0' }, { label: '公司', value: '1' }],
        curTab: '0',
        tabRight: '',
        grayCardList: [{ label: '本月分账', value: '', unit: '', color: '' }, { label: '本年分账', value: '', unit: '', color: '' }, { label: '累计分账', value: '', unit: '', color: '' }],
        barText: '分账金额',
        unitText: '',
        updateText: '',
        echartData: {},
        startDate: '',
        endDate: '',
    }
)
const getCardData = () => {
    dashBoardApi.facilitatorCardApi({ companyPlatformId: props.companyPlatformId, dataTime: props.updateDate, cardFlat: card.curTab }).then((res) => {
        if (res.data.code == 200) {
            card.tabRight = '截至 ' + res.data.data.theDate
            card.grayCardList[0].value = toThousands(res.data.data.servicePriceMonth)
            card.grayCardList[1].value = toThousands(res.data.data.servicePriceYear)
            card.grayCardList[2].value = toThousands(res.data.data.servicePriceSum)
            card.unitText = res.data.data.valueUnit
        }
    }).catch(err => {
        console.log(err)
    });
}
const getChartData = () => {
    dashBoardApi.facilitatorChartsApi({ companyPlatformId: props.companyPlatformId, dataTime: props.updateDate, cardFlat: card.curTab }).then((res) => {
        if (res.data.code == 200) {
            card.echartData = res.data.data
        }
    }).catch(err => {
        console.log(err)
    });
}
const goDetail = () => {
    // reportLog({
    //     type: 4,
    //     resourceKey: permi,
    // })
    router.push({
        path: '/page/routeController/index',
        query: {
            rid: 'EnterpriseServices/detailPages/ServiceAssay/ServiceAssayDetail',
            rTitle: '分账详情',
            companyPlatformId: props.companyPlatformId,
            startDate: card.startDate,
            endDate: card.endDate,
            updateDate: props.updateDate,
        },
    })
}
const changeTab = (value) => {
    // 切换tab

    if (value == '0') {
        card.grayCardList = [{ label: '本月分账', value: '', unit: '', color: '' }, { label: '本年分账', value: '', unit: '', color: '' }, { label: '累计分账', value: '', unit: '', color: '' }]
        card.barText = '分账金额'
    }
    if (value == '1') {
        card.grayCardList = [{ label: '注册公司', value: '', unit: '', color: '' }, { label: '本年分账', value: '', unit: '', color: '' }, { label: '累计分账', value: '', unit: '', color: '' }]
        card.barText = '分账金额'
    }

    const monthsAgo = new Date(props.updateDate)
    monthsAgo.setMonth(monthsAgo.getMonth() - 6)
    card.startDate = dateFormat(monthsAgo, 'YYYY-MM')
    card.endDate = props.updateDate.slice(0,7)
    
    card.curTab = value

    getCardData()
    getChartData()
}
changeTab('0')

const biProjectCode = computed(() => {
    return props.biProjectCode
});
watch(biProjectCode, () => {
    changeTab('0')
}, { deep: true });

</script>

<style scoped lang="scss"></style>
